package com.ys.controller.api.eKnowAi;

import com.alibaba.fastjson.JSONObject;
import com.ys.entity.eKnowAi.*;
import com.ys.enume.eKnowAi.EknowCodeEnum;
import com.ys.service.eKnowAi.EKnowTemplateService;
import com.ys.service.sys.CommonApiService;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.LayuiTableVoJA;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/inter/eKnowTemplate")
public class ApiInterEKnowTemplateController {
	private static final Logger logger = LoggerFactory.getLogger(ApiInterEKnowTemplateController.class);


	@Autowired
	private CommonApiService<EKnowTemplate> commonApiService;

	@Autowired
	private EKnowTemplateService eKnowTemplateService;


	/**
	 * 设置查询的cond的sql设置
	 * @param cond
	 * @return
	 */
	public EKnowTemplate setSeachSql(EKnowTemplate cond){
		cond.addField("A.UserId + '/' + U.UserName AS userName");//操作人
		cond.addField("T.TypeName AS typeName");//分类名称
		cond.addLeftJoin("kjecp.dbo.TS_UserInfo U ON U.UserId = A.UserId");
		cond.addLeftJoin("kjecp_eknow.dbo.EKnow_TemplateType T ON T.Id = A.TypeId");
		//排序
		cond.setOrderby("A.UpdateTime DESC");
		return  cond;
	}


	/**
	 * 查询
	 */
	//@RequiresPermissions("eKnowTemplate:view")
	@RequestMapping("/search")
	@ResponseBody
	public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
								 @ModelAttribute EKnowTemplate cond
	){
		try {
			cond = setSeachSql(cond);
			LayuiTableVoJA tableVoJA = commonApiService.searchLay(cond);
			return tableVoJA;
		}catch (Exception e){
			logger.error("查询出错：",e.getMessage(),e);
			LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
			tableVoJA.setCode(1);
			tableVoJA.setMsg("查询出错，请稍后重试！");
			return tableVoJA;
		}
	}


	/**
	 *	查询提示词模版信息根据id
	 */
	@PostMapping("/searchEKnowTemplateById")
	@ResponseBody
	public JSONObject searchEKnowTemplateById(HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		JSONObject result = new JSONObject();
		try {
			long id = Long.parseLong(params.get("id").toString());//提示词模版Id
			EKnowTemplate eKnowTemplate = new EKnowTemplate().setId(id).queryFirst();
			List<EKnowTemplateType> eKnowTemplateTypeList = new EKnowTemplateType().setSceneId(eKnowTemplate.getSceneId()).setOrderby("A.Sort ASC").queryList();
			result.put("code", EknowCodeEnum.ApiRetuenCode.SUCCESS);
			result.put("eKnowTemplate", eKnowTemplate);
			result.put("eKnowTemplateTypeList", eKnowTemplateTypeList);
		}catch (Exception e) {
			result.put("code", EknowCodeEnum.ApiRetuenCode.FAIL);
			logger.error("查询提示词模版信息出错：",e.getMessage(),e);
		}
		return result;
	}




	/**
	 * 新增、修改提示词模版
	 */
	@PostMapping("/addOrUpdateEKnowTemplate")
	@ResponseBody
	public JSONObject addOrUpdateEKnowTemplate(HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		JSONObject result = new JSONObject();
		try {
			long sceneId = Long.parseLong(params.get("sceneId").toString());//场景Id
			long typeId = Long.parseLong(params.get("typeId").toString());//所属分类id
			String templateName = params.get("templateName").toString();//标题
			String templateContent = params.get("templateContent").toString();//内容
			int sort = Integer.parseInt(params.get("sort").toString());//排序
			String loginUserId = ShiroUtils.getUserId();//登录的用户ID

			EKnowTemplate eKnowTemplate = new EKnowTemplate();
			eKnowTemplate.setSceneId(sceneId);
			eKnowTemplate.setTypeId(typeId);
			eKnowTemplate.setTemplateName(templateName);
			eKnowTemplate.setTemplateContent(templateContent);
			eKnowTemplate.setSort(sort);
			eKnowTemplate.setUserId(loginUserId);

			int isSave = 0;
			EKnowScene eKnowScene = new EKnowScene().setId(sceneId).queryFirst();
			if(eKnowScene!=null){
				if(params.get("addOrUpdate").toString().equals("add")) {//新增
					isSave = eKnowTemplateService.add(eKnowTemplate);
				}else {//修改
					eKnowTemplate.setId( Long.parseLong(params.get("id").toString()));
					isSave = eKnowTemplateService.update(eKnowTemplate);
				}
				if(isSave>0){
					result.put("code", EknowCodeEnum.ApiRetuenCode.SUCCESS);
				}else {
					result.put("code", EknowCodeEnum.ApiRetuenCode.FAIL);
				}
			}else {
				result.put("code", EknowCodeEnum.ApiRetuenCode.NOBASE);
			}
		}catch (Exception e) {
			result.put("code", EknowCodeEnum.ApiRetuenCode.FAIL);
			logger.error("逸知道新增、修改提示词模版出错：",e.getMessage(),e);
		}
		return result;
	}



	/**
	 * 删除提示词模版
	 */
	@PostMapping("/delEKnowTemplate")
	@ResponseBody
	public JSONObject delEKnowTemplate(HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		JSONObject result = new JSONObject();
		try {
			long id = Long.parseLong(params.get("id").toString());//Id
			int isDel = eKnowTemplateService.delete(id);
			if(isDel>0){
				result.put("code", EknowCodeEnum.ApiRetuenCode.SUCCESS);
			}else {
				result.put("code", EknowCodeEnum.ApiRetuenCode.FAIL);
			}

		}catch (Exception e) {
			result.put("code", EknowCodeEnum.ApiRetuenCode.FAIL);
			logger.error("逸知道删除提示词模版出错：",e.getMessage(),e);
		}
		return result;


	}

	/**
	 * 批量删除提示词模版
	 */
	@PostMapping("/delEKnowTemplateByBatch")
	@ResponseBody
	public JSONObject delEKnowTemplateByBatch(HttpServletRequest request, HttpServletResponse response,@RequestParam("list")List<String> strList){
		JSONObject result = new JSONObject();
		try {
			int isDels = 0;
			for(String item:strList){
				long id = Long.parseLong(item);//Id
				int isDel = eKnowTemplateService.delete(id);
				if(isDel>0){
					isDels++;
				}
			}
			if(isDels>0){
				result.put("code", EknowCodeEnum.ApiRetuenCode.SUCCESS);
			}else {
				result.put("code", EknowCodeEnum.ApiRetuenCode.FAIL);
			}

		}catch (Exception e) {
			result.put("code", EknowCodeEnum.ApiRetuenCode.FAIL);
			logger.error("逸知道批量删除提示词模版出错：",e.getMessage(),e);
		}
		return result;


	}




}